;--------------------------------------------------------------------
;GPIO configuration:
;--------------------------------------------------------------------
;GP0 - Piezo output
;GP1 - Option switch
;GP2 - (no pull-up)m Power detect
;GP3 - (only input) Load detect
;--------------------------------------------------------------------
;

#include <p10f200.inc>        ; processor specific variable definitions

#define	NO_PARAMS	H'03'

	__CONFIG   _MCLRE_OFF & _CP_OFF & _WDT_OFF

;----------------------------------
;registers define
;----------------------------------
cnt1		equ     H'10'
cnt2		equ     H'11'
cnt3		equ     H'12'	;power-on power-off load-on load-off loading
state	    equ     H'13'	;state register
;frequency
timp	    equ     H'14'	;time of pulse high or low
ts		    equ     H'15'	;time of signal
tp		    equ     H'16'	;time of pause
sound		equ		H'17'	;sound register
;loaded sample from flash
inmasq		equ		H'18'	;parameter of detection; nr of input
tstimes		equ		H'19'	;timer for periodical signal during load on/power on
;----------------------------------
;Bits definition on State register
PS			equ		0		;power state
PSO			equ		1		;old power state
LS			equ		2		;load state
LSO			equ		3		;old load state
OS			equ		4		;external option switch state
OSO			equ		5		;old external option switch state
;Bits definition on Sound register
PR			equ		0		;power raise sound
PF			equ		1		;power fall sound
LR			equ		2		;load raise sound
LF			equ		3		;load fall sound
TS			equ		4		;time sound
;----------------------------------
;gpio definition

PIEZO_OUT_PIN		equ		2
LOAD_DET_PIN		equ		0
POWER_DET_PIN		equ		3
SWITCH_OPT_PIN		equ		1

TIME_PERIOD_SOUND	equ		D'195'		;period = (0,85s * (255-TIME_PERIOD_SOUND)) = 51 [s]
NO_LAPS				equ		D'20'		;number of positive sine of detection procedure
MIN_HIGH_POWER		equ		~D'05'+1	;minimal power 5 high / 20 (U2 notation)
;MIN_HIGH_LOAD		equ		~D'20'+1	;minimal load 4 high / 20
MIN_HIGH_LOAD		equ		D'10'		;(not included) minimal load 4 high / 20
MAX_HIGH_LOAD		equ		(NO_LAPS - D'0') ;(included)
;----------------------------------

RES_VECT	CODE    0x0000
	goto		init
;===============================================================================
MAIN_PROG CODE                      ; let linker place main program

#include "sound.inc"
#include "delays.inc"
#include "detection.inc"


;initialization
init:

	movlw	000h
	movwf   OSCCAL            ; update register with factory cal value

	clrwdt
	clrf	TMR0
	MOVLW	b'10000111'
	OPTION

	clrf 	GPIO			;clear output pins
	movlw 	(1<<LOAD_DET_PIN)|(1<<POWER_DET_PIN)|(1<<SWITCH_OPT_PIN) ;set input pins
	tris 	GPIO

	clrf	state
	clrf	sound

;--------------------------------------------------------------------
main:
	rlf		state,F					;save currently state as old state

	;detect power state PS
	movlw	0xFF & 1<<POWER_DET_PIN	;set power detect input pin
	movwf	inmasq					;move to parameter
	call	detection_init			;call function, cnt2 is returned value of high
	;movf	cnt2,F					;test if zero (or window)
	movlw	MIN_HIGH_POWER		;move U2 complement value of minimal high counts
	addwf	cnt2,W					;add it
	btfss	STATUS,C				;if carry is set means power is on
	goto	detect_power_state_low
detect_power_state_high:
	bsf		state,PS
	goto	detect_power_state_end
detect_power_state_low:
	bcf		state,PS
detect_power_state_end:

	;detect load state LS
	movlw	0xFF & 1<<LOAD_DET_PIN	;set load detect input pin
	movwf	inmasq					;move value to parameter
	call	detection_init			;call function
	;movf	cnt2,F					;test if zero (or window)

	movlw	MIN_HIGH_LOAD			;move U2 complement value of minimal high counts
	;addwf	cnt2,W					;add it, check if number of count is greater than constant
	subwf	cnt2,W
	btfss	STATUS,C				;if carry is set means power is on
	goto	detect_load_state_low
;detect_load_state_high_1:
;	movlw	MAX_HIGH_LOAD
;	subwf	cnt2,W
;	btfsc	STATUS,C
;	goto	detect_load_state_low
detect_load_state_high:
	bsf		state,LS				;set load flag
	goto	detect_load_state_end
detect_load_state_low:
	bcf		state,LS				;clear load flag
detect_load_state_end:



	;detect state of switch
	btfsc	GPIO,SWITCH_OPT_PIN					;copy value of switch state
	bsf		state,OS				;this copy protect for changing state of switch (distortion, osc)
	btfss	GPIO,SWITCH_OPT_PIN
	bcf		state,OS
detect_switch_end:
;-------------------------------------------------------------------------------
;Detect if any events occured
;-------------------------------------------------------------------------------
	;check if counter overlap
	incf	tstimes,F				;increment for periodic sound signal;
	btfsc	STATUS,Z				;
	bsf		sound,TS
	;check if state of switch was changed
	btfsc	state,OS
	goto	check_switch_changed_high
check_switch_changed_low:
	btfsc	state,OSO
	bsf		sound,TS
	goto	check_switch_changed_end
check_switch_changed_high:
	btfss	state,OSO
	bsf		sound,TS
check_switch_changed_end:

;check power sound signals--------------------------------------------------
	btfss	state,PS
	goto	check_ps_sound_clear
check_ps_sound_set:
	btfsc	state,PSO
	goto	check_ps_sound_end
	bsf		sound,PR
check_ps_sound_clear:
	btfsc	state,PSO
	bsf		sound,PF
check_ps_sound_end:

;check load sound signals---------------------------------------------------
	btfss	state,LS
	goto	check_ls_sound_clear
check_ls_sound_set:
	btfsc	state,LSO
	goto	check_ls_sound_end
	bsf		sound,LR
check_ls_sound_clear:
	btfsc	state,LSO
	bsf		sound,LF
check_ls_sound_end:

;-------------------------------------------------------------------------------
;High level logic of sound
;-------------------------------------------------------------------------------
	;if load on or power on and LR sound sheduled (LR) then restart periodic signal
	btfsc	sound,PR
	goto	high_logic_cond2_l1
	btfss	sound,LR
	goto	high_logic_cond2_end
high_logic_cond2_l1:
	movlw	TIME_PERIOD_SOUND		;reset periodic signal
	movwf	tstimes
	bcf		sound,TS
high_logic_cond2_end:

	;if no power clear all signals exepct power fall PF
	movlw	B'00000010'
	btfss	state,PS
	andwf	sound,F

	;if switch is off (low) clear load sounds (LF,LR)
	movlw	B'11110011'
	btfss	state,OS
	andwf	sound,F

	;if switch is on and no load clear TS
	btfss	state,OS
	goto	high_logic_cond1_end
	btfsc	state,LS
	goto	high_logic_cond1_end
	bcf		sound,TS
high_logic_cond1_end:

	;if TS is set set counter value to TIME_PERIOD_SOUND
	btfss	sound,TS
	goto	set_period_couinter_value_end
	movlw	TIME_PERIOD_SOUND		;reset periodic signal
	movwf	tstimes
set_period_couinter_value_end:
;-------------------------------------------------------------------------------
;Sound executing
;-------------------------------------------------------------------------------
	;call sound signals
	btfsc	sound,PF				;check for Power Raise signal
	goto	call_sound_PF
	btfsc	sound,PR				;check for Power Raise signal
	goto	call_sound_PR
	btfsc	sound,LR				;check for Power Fall signal
	goto	call_sound_LR
	btfsc	sound,LF				;check for Power Fall signal
	goto	call_sound_LF
	btfsc	sound,TS				;check for Period sound signal
	goto	call_sound_TS
	goto	call_sound_signals_end
	;check for Load Raise signal
call_sound_PF:						;power off
	callsample	SMP02
	callsample	SMP02
	callsample	SMP01
	bcf		sound,PF
	goto	call_sound_signals_end
call_sound_PR:						;power on
	callsample	SMP01
	callsample	SMP01
	callsample	SMP02
	bcf		sound,PR
	goto	call_sound_signals_end
call_sound_LR:						;load connected
	callsample	SMP01
	callsample	SMP02
	bcf		sound,LR
	goto	call_sound_signals_end
call_sound_LF:						;load disconnect
	callsample	SMP02
	callsample	SMP01
	bcf		sound,LF
	goto	call_sound_signals_end
call_sound_TS:						;period signal
	callsample	SMP02
	bcf		sound,TS
	goto	call_sound_signals_end
call_sound_signals_end:
	goto	main

	END
